/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-16
 * Time: 0:08
 */

//继承 Exception 就是受查异常
class UserException1 extends Exception {
    public UserException1(String message) {
        super(message);
    }
}

//继承 RuntimeException 就是非受查异常
class PasswordException1 extends RuntimeException{
    public PasswordException1(String message){
        super(message);
    }
}

public class TestDemo2 {
    private static void testMethod(){

        System.out.println("testMethod");

    }

    public static void main(String[] args) {

        ((TestDemo2)null).testMethod();

    }

    public static void main4(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[1]);
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("123");
        }
    }

    public static void main3(String[] args) {
        int i = 0;
        while (i<10){
            try {
                if(i<10);
                throw new Exception();
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("尝试连接网络"+i+"次");
                i++;
            }
        }
        System.out.println("终于连接到了网络，程序继续执行！");
    }

    public static void main2(String[] args) {
        try {
            throw new Exception("抛出异常！");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("finally被执行了！");
        }
    }

    public static String Username = "admin";
    public static String Password = "123456";

    public static void main1(String[] args)  {
        try {
            login("admin","1234");
        }catch (UserException e){
            e.printStackTrace();
            System.out.println("用户名错误了！");
        }catch (PasswordException e){
            e.printStackTrace();
            System.out.println("登录密码错误了！");
        }finally {
            System.out.println("finally执行了！");
        }
    }
    public static void login(String username,String password) throws UserException, PasswordException {
        if(!TestDemo2.Username.equals(username)){
            throw new UserException("用户名错误");
        }
        if(!TestDemo2.Password.equals(password)){
            throw new PasswordException("登录密码错误");
        }
        System.out.println("登录成功！");
    }
}
